package br.com.cliente;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Date;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import br.com.cliente.exception.ConnectionLost;
import br.com.cliente.exception.NoConnectionServer;
import br.com.cliente.exception.TimeOutReceive;
import br.com.cliente.sessao.Sessao;
import br.com.cliente.sessao.SessaoUsuario;
import br.com.comunications.beans.Cliente;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException, InterruptedException {
		
//		IoConnector connector = new NioSocketConnector();
//		connector.getSessionConfig().setReadBufferSize(2048);
//
//		connector.getFilterChain().addLast("logger", new LoggingFilter());
//		connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
//
//		connector.setHandler(new ClientHandler());
//		ConnectFuture future = connector.connect(new InetSocketAddress("127.0.0.1", 2503));
//		future.awaitUninterruptibly();
//
//		
//		
//		
////		IoSession session = future.getSession();
//		
//		
////		session.getConfig().setUseReadOperation(true);
////		session.getCloseFuture().awaitUninterruptibly();
//
//		System.out.println("After Writing");
//		connector.dispose();
		
		try {
			
			Cliente object = new Cliente();
			object.setNome("alguem");
			object.setAlgo("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXASAFSDA ASD ASDASD ASD ASD ASD ASD ASD ASD ASDASDAS ".getBytes());
			
			
			Sessao sessao = SessaoUsuario.getInstance();
			
			try {
				sessao.EnviaMenssagem(object);
			} catch (ConnectionLost e) {
				e.printStackTrace();
			} catch (TimeOutReceive e) {
				e.printStackTrace();
			}
			
//			
//			try {
//				sessao.EnviaMenssagem(object);
//			} catch (ConnectionLost e) {
//				e.printStackTrace();
//			} catch (TimeOutReceive e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
		} catch (NoConnectionServer e) {
			e.printStackTrace();
		}
		
	}
}
